package com.itany.corejava.code12_线程.zuoye;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年12月01日 9:24
 */
public class Test02 {
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread t1=new Thread(mt);
        Thread t2=new Thread(mt);
        t1.setName("抽奖箱1");
        t2.setName("抽奖箱2");
        t1.start();
        t2.start();
    }
}
class MyThread implements Runnable{
    int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
    List<Integer> list=new ArrayList<>();
    @Override
    public void run() {
        while(true){
            synchronized (this){
                Random random=new Random();
                int i=random.nextInt(arr.length);//随机索引值  2
                if(!list.contains(arr[i])){
                    list.add(arr[i]);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"产生了一个大奖:"+arr[i]);
                }
                if(list.size()==arr.length){
                    break;
                }



            }
        }
    }
}